//
//  MBDGroupCommentsReplyCellModel.swift
//  MasterBoy
//
//  Created by kyson on 2024/9/20.
//

import Foundation

import YYText


class MBDGroupCommentsReplyCellModel : MBDCommonCellModel {
    var groupPosterUserNameLayout : YYTextLayout?
    var groupCommentsLayout : YYTextLayout?
    var groupCommentsTimeLayout : YYTextLayout?
    var posterAvatar : String?
    var posterRealName : String?
    var posterUserId : String?
    var likeCount : String?
    var liked : Bool!
    var avatarClicedBlock : KGroupCommentsReplyAvatarClickedBlock?
    var replyButtonClicedBlock : KGroupCommentsReplyReplyButtonClickedBlock?
    var replyPraiseButtonClicedBlock : KGroupCommentsReplyReplyButtonClickedBlock?

    static let MarginCommentsLabelLeftAndRight : CGFloat = 7
    static let avatarWidth : CGFloat = 24
    static let avatarMarginLeftToSuperView : CGFloat = 35
    static let marginLeftAvatarAndPoster : CGFloat = 8
    static let avatarMarginTop : CGFloat = 8
    static let avatarPosterTop : CGFloat = 11
    static let posterMarginRight : CGFloat = 7
    static let praiseButtonWidth : CGFloat = 33
    static let commentsMarginTop : CGFloat = 8
    static let commentsTimeMarginTop : CGFloat = 8
    static let marginBottom : CGFloat = 6

    var reply: MBDGroupCommentsReplyModel! {
        didSet {
            //发布人
            var poster = self.reply.realName ?? "无名氏"
            if poster.isBlank {
                poster = "无名氏"
            }
            let posterAttributeString = NSMutableAttributedString(string: poster )
            posterAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            posterAttributeString.yy_lineSpacing = 4
            posterAttributeString.setDynamicTextColor()

            if let replyto = self.reply.replyRealName {
                let rpy2AttributeString = NSMutableAttributedString(string: replyto)
                rpy2AttributeString.yy_font = UIFont.systemFont(ofSize: 14)
                rpy2AttributeString.yy_lineSpacing = 4
                rpy2AttributeString.setDynamicTextColor()

                let ptyAttributeString = NSMutableAttributedString(string: " 回复 " )
                ptyAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
//                ptyAttributeString.yy_color = UIColor.darkGray
                ptyAttributeString.setDynamicTextColor()
                ptyAttributeString.yy_lineSpacing = 4
                
                posterAttributeString.append(ptyAttributeString)

                posterAttributeString.append(rpy2AttributeString)
            }

            let posterContainer = YYTextContainer()

            let posterWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDGroupCommentsReplyCellModel.MarginLeftAndRightBackgroundView * 2 - MBDGroupCommentsReplyCellModel.avatarMarginLeftToSuperView - MBDGroupCommentsReplyCellModel.avatarWidth - MBDGroupCommentsReplyCellModel.marginLeftAvatarAndPoster  - MBDGroupCommentsReplyCellModel.posterMarginRight - MBDGroupCommentsReplyCellModel.praiseButtonWidth
            
            posterContainer.size = CGSizeMake(posterWidth , CGFLOAT_MAX)
            let posterLayout = YYTextLayout(container: posterContainer, text: posterAttributeString)

            self.groupPosterUserNameLayout = posterLayout
            
            //评论
            let comments = self.reply.comment ?? ""
            let commentsAttributeString = NSMutableAttributedString(string: comments )
            commentsAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            commentsAttributeString.yy_lineSpacing = 4
            commentsAttributeString.setDynamicTextColor()

            let commentsContainer = YYTextContainer()
            let commentsWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDGroupCommentsReplyCellModel.MarginLeftAndRightBackgroundView * 2 - MBDGroupCommentsReplyCellModel.avatarMarginLeftToSuperView - MBDGroupCommentsReplyCellModel.avatarWidth - MBDGroupCommentsReplyCellModel.marginLeftAvatarAndPoster - MBDGroupCommentsReplyCellModel.praiseButtonWidth
            
            commentsContainer.size = CGSizeMake(commentsWidth , CGFLOAT_MAX)
            let commentsLayout = YYTextLayout(container: commentsContainer, text: commentsAttributeString)

            self.groupCommentsLayout = commentsLayout
            
            //评论时间
            let commentsTime = MBDTimeUitls.time2CurrentTime(time: self.reply.createTime!)
            let timeAttributeString = NSMutableAttributedString(string: commentsTime )
            timeAttributeString.yy_font = UIFont.systemFont(ofSize: 12)
//            timeAttributeString.yy_color = UIColor.lightGray
            timeAttributeString.setDynamicTextColor()
            timeAttributeString.yy_lineSpacing = 4

            let timeContainer = YYTextContainer()

            let commentsTimeWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDGroupCommentsReplyCellModel.MarginLeftAndRightBackgroundView * 2 - MBDGroupCommentsReplyCellModel.avatarMarginLeftToSuperView - MBDGroupCommentsReplyCellModel.avatarWidth - MBDGroupCommentsReplyCellModel.marginLeftAvatarAndPoster
            timeContainer.size = CGSizeMake(commentsTimeWidth , CGFLOAT_MAX)
            let commentsTimeLayout = YYTextLayout(container: timeContainer, text: timeAttributeString)

            self.groupCommentsTimeLayout = commentsTimeLayout
            
            self.posterAvatar = MBDPath2URLUtils.wrapAvatarURL(path: self.reply.avatarUrl)
            self.posterRealName = self.reply.realName ?? "无名氏"
            
            self.posterUserId = self.reply.userId
            self.likeCount = Int(self.reply.likeCount)! < 1 ? "" : self.reply.likeCount
            self.liked = self.reply.isLike
            
        }
    }
    
    override func cellHeight() -> CGFloat {
        return MBDGroupCommentsReplyCellModel.avatarPosterTop +  (self.groupPosterUserNameLayout?.textBoundingSize.height ?? 0) + MBDGroupCommentsReplyCellModel.commentsMarginTop +  (self.groupCommentsLayout?.textBoundingSize.height ?? 0) + MBDGroupCommentsReplyCellModel.commentsTimeMarginTop + (self.groupCommentsTimeLayout?.textBoundingSize.height ?? 0) + MBDGroupCommentsReplyCellModel.marginBottom
    }
}
